<?php
use app\easyreportphp\Model\MetaCategory as MetaCategoryModel;
use app\easyreportphp\Model\MemberModule as MemberModuleModel;
use app\easyreportphp\Model\MetaDatasource as MetaDatasourceModel;
use app\easyreportphp\Model\MetaReport as MetaReportModel;
//获取左侧分类
function getMetaCategoryMenu()
{ 
	$list = collection(MetaCategoryModel::all()) ->toArray();
	return getMetaCategoryTree($list,array(),0);	
}
function getMetaCategoryTree($data,$res=array(),$pid=0)
{ 
	
	foreach ($data as $k => $v){
        if($v['parentId']==$pid){  
			$bean['id'] =$v['id'];
			$bean['pid'] =$v['parentId'];
			$bean['text'] =$v['name'];
			$bean['state'] ='open';
			$bean['iconCls'] ='icon-category';
			$bean['checked'] = false;
            $bean['attributes']=$v;
			$bean['children']= getMetaCategoryTree($data,array(),$v['id']);
			$res[] =$bean; 	 
					
			 
        }
    }
    return $res;
}
//获取菜单分类
function getMenus()
{  
	$list = collection(MemberModuleModel::order('sequence asc')->select()) ->toArray();
	return getMenusTree($list,array(),0);	
}
function getMenusTree($data,$res=array(),$pid=0)
{   
	foreach ($data as $k => $v){
        if($v['parentId']==$pid){  
			//处理前端的一些问题原版在处理类似 has_child 到了前端都转换成了hasChild大写了 后面有问题可以再做适配 
			//$v['hasChild'] = $v['has_child'];已在数据库中改名了
			$bean['id'] =$v['id'];
			$bean['pid'] =$v['parentId'];
			$bean['text'] =$v['name'];  
			$bean['state'] = $pid==0?'closed':'open';
			$bean['iconCls'] =$v['icon'];
			$bean['checked'] = false;
            $bean['attributes']=$v;
			$bean['children']= getMenusTree($data,array(),$v['id']);
			$res[] =$bean;  
        }
    }
    return $res;
}
//获取数据源(TODO 此处单独处理一下 把敏感数据过滤掉)
function getMetaDatasource()
{  
	$list = collection(MetaDatasourceModel::order('id asc')->select()) ->toArray();
	return $list;	
}
//获取报表
function getMetaReport($page,$pagesize)
{  
	$list = collection(MetaReportModel::order('id asc')->select()) ->toArray();
	foreach ($list as $k => $v)
	{
		$list[$k]['dsName'] = MetaDatasourceModel::get(['id' => $v['dsId']])->name;
		
	}
	$ret['rows'] = $list;
	$ret['total'] = count($list);
	return $ret;	
}